package com.yiren.manager.ctrl.lease;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.Favorites;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrLeaseSupply;
import com.yiren.entity.YrRetreatLease;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.lease.YrRetreatLeaseForm;
import com.yiren.manager.service.iface.lease.YrRetreatLeaseService;
import com.yiren.manager.service.iface.pension.YrLeaseSupplyService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("retreatLease")
@Controller
public class YrRetreatLeaseController extends GenericController<YrRetreatLeaseForm, YrRetreatLease> {

	@Autowired
	private YrRetreatLeaseService selfService;
	
	@Autowired
	private YrLeaseSupplyService leaseSupplyService;
	
	// 保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrRetreatLeaseForm> save(String data) throws SwallowException, MyException, InstantiationException,
			IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
		HTTPResult<YrRetreatLeaseForm> result = new HTTPResult<YrRetreatLeaseForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrRetreatLeaseForm form = tool.mapper("retreatLease", YrRetreatLeaseForm.class);
		if (null == form) {
			throwExp("param.error");
		}
		if (null == form.getRetreatLeaseId() || form.getRetreatLeaseId().isEmpty()) {
			form.setRetreatLeaseId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrRetreatLease entity = ObjectUtils.objectToObject(form, YrRetreatLease.class);
		
//		YrLeaseSupply leaseSupply = leaseSupplyService.findOne(form.getLeaseSuppliesId());
//		if(form.getStatus()==1){
//			if(null==leaseSupply){
//				throwExp("没有该租赁用品");
//			}
//			leaseSupply.setStockNumber(leaseSupply.getStockNumber()+1);
//			leaseSupply.setLeaseNumber(leaseSupply.getLeaseNumber()-1);
//		}
//		Favorites favorites = new Favorites();
//		favorites.putFavorite(YrLeaseSupply.class, leaseSupply);
//		favorites.putFavorite(YrRetreatLease.class, entity);
		
		selfService.save(entity);

		return result;
	}
	
	/**
	 * 查询所有
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrRetreatLeaseForm> findAll() throws Exception{
		HTTPResult<YrRetreatLeaseForm> result = new HTTPResult<YrRetreatLeaseForm>();
		
		String sqlWhere = "status=0";
		QueryResult<YrRetreatLease> qr = selfService.find(YrRetreatLease.class, sqlWhere, new Object[]{});
		List<YrRetreatLease> entityList = qr.getResultList();
		List<YrRetreatLeaseForm> formList = new ArrayList<YrRetreatLeaseForm>();
		for (YrRetreatLease entity : entityList) {
			YrRetreatLeaseForm form = toForm(entity);
			formList.add(form);
		}
		
		result.setData(formList);
		return result;
	}
	
	@Override
	public YrRetreatLeaseForm toForm(YrRetreatLease e) throws Exception {
		YrRetreatLeaseForm form = ObjectUtils.objectToObject(e, YrRetreatLeaseForm.class);
		return form;
	}

	@Override
	public YrRetreatLease toEntity(YrRetreatLeaseForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
